package com.example.jpa.service.impl;

import com.example.jpa.repository.BrandRepository;
import com.example.jpa.repository.RougeRepository;
import com.example.jpa.service.RougeService;
import com.example.jpa.service.dto.BrandInfo;
import com.example.jpa.service.dto.RougeInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;

/**
 * @author : zhayh
 * @date : 2021-4-4 12:47
 * @description :
 */

@Service(value = "rougeJpaService")
public class RougeServiceJpaImpl implements RougeService {
    private final BrandRepository brandRepository;
    private final RougeRepository rougeRepository;

    // 构造方法注入
    public RougeServiceJpaImpl(BrandRepository brandRepository,
                               RougeRepository rougeRepository) {
        this.brandRepository = brandRepository;
        this.rougeRepository = rougeRepository;
    }

    @Override
    public List<BrandInfo> getAllBrandInfo() {
        return brandRepository.findAll();
    }

    @Override
    public RougeInfo getRougeById(Long id) {
        return rougeRepository.findById(id).orElse(null);
    }

    @Override
    public RougeInfo addRouge(RougeInfo rougeInfo) {
        // 保存单条记录
        return rougeRepository.save(rougeInfo);
    }

    @Transactional
    @Override
    public RougeInfo updateRougePrice(Long id, Float price) {
//        Optional<RougeInfo> optional = rougeRepository.findById(id);
//        if (optional.isPresent()) {
//            RougeInfo rougeInfo = optional.get();
//            rougeInfo =  rougeRepository.save(rougeInfo);
//            return rougeInfo;
//        }
        RougeInfo rougeInfo = rougeRepository.findById(id).orElse(null);
        if (rougeInfo != null) {
            if (rougeRepository.updateRougePrice(id, price) > 0) {
                rougeInfo.setRougePrice(price);
            }
        }
        return rougeInfo;
    }

    @Override
    public Boolean deleteRouge(Long id) {
        try {
            // 根据主键删除单条记录
            rougeRepository.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 批量更新
    @PersistenceContext
    private EntityManager entityManager;

    @Transactional
    public void batchUpdate(List<RougeInfo> rougeInfos) {
        rougeInfos.forEach(rougeInfo -> {
            entityManager.merge(rougeInfo);
        });
        entityManager.flush();
        entityManager.clear();
    }

    // 分页查询
    @Override
    public Page<RougeInfo> getRougeInfoByKeyword(String keyword, Integer pageIndex, Integer pageSize) {
        return rougeRepository.selectByKeyword(keyword, PageRequest.of(pageIndex, pageSize));
//        return rougeInfoRepository.findByRougeNameLikeOrBrandInfoBrandNameLikeOrShopInfoShopNameLike(
//                keyword, keyword, keyword, PageRequest.of(pageIndex, pageSize));
    }
}
